package csc.fresher.service.impl;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import csc.fresher.constant.ProjectBankingConstant;
import csc.fresher.dao.CurrencyDao;
import csc.fresher.dao.InterestRateDao;
import csc.fresher.dao.PeriodTypeDao;
import csc.fresher.dao.impl.SavingAccountHasInterestRateDaoImpl;
import csc.fresher.entity.AccountHasInterestRate;
import csc.fresher.entity.Currency;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.InterestRate;
import csc.fresher.entity.PeriodType;
import csc.fresher.entity.SavingTransactionType;
import csc.fresher.factory.InterestRateDaoFactory;
import csc.fresher.service.InterestRateService;

@Service
public class SavingInterestRateServiceImpl implements InterestRateService{

	@Autowired
	InterestRateDaoFactory interestRateDaoFactory;
	@Autowired
	CurrencyDao currencyDao;
	@Autowired
	PeriodTypeDao periodTypeDao;
	@Autowired
	SavingAccountHasInterestRateDaoImpl savingAccountHasInterestRateDaoImpl;
	private InterestRateDao interestRateDao;
	
	@PostConstruct
	public void init() {
		interestRateDao = interestRateDaoFactory.getType(ProjectBankingConstant.SAVING_ACCOUNT_TYPE);
	}
	
	@Override
	public List<InterestRate> getAllInterestRate() {
		List<InterestRate> interestRates = interestRateDao.getAllInterestRates();
		return interestRates;
	}

	@Override
	public InterestRate getInterestRateByID(int interestRateID) {
		InterestRate interestRate = null;
		if(interestRateDao != null)
			interestRate = interestRateDao.getInterestRateByID(interestRateID);
		return interestRate;
	}
	
	@Override
	public List<Currency> getAllCurrency() {
		List<Currency> currencies = currencyDao.getAllCurrency();
		return currencies;
	}

	@Override
	public List<PeriodType> getAllPeriodTypes() {
		List<PeriodType> periodTypes = periodTypeDao.getAllPeriodTypes();
		return periodTypes;
	}

	@Override
	public InterestRate getInterestRate(int currencyID, int periodTypeID) {
		InterestRate interestRate = interestRateDao.getInterestRateByType(currencyID, periodTypeID);
		if(interestRate != null) return interestRate;
		return null;
	}

	@Override
	public double calculateAfterBalance(CustomerAccount customerAccount, double funds,
			double interestRate, int periodTypeID, SavingTransactionType savingTransactionType) {
		int numberDays = getNumberOfDays(periodTypeID);
		System.out.println("rate/week: " + interestRate/48 + " weeks: " + numberDays);
		double currentBalance = customerAccount.getAccountBalance();
		double amountTemp = 0;
		if (savingTransactionType == SavingTransactionType.SAVING) {
			amountTemp = currentBalance + funds;
			return amountTemp + amountTemp*numberDays*interestRate/36000;
		}else if(savingTransactionType == SavingTransactionType.WITHDRAW_ALL){
			return 0;
		}else if(savingTransactionType == SavingTransactionType.WITHDRAW_INTEREST){
			AccountHasInterestRate accountHasInterestRate = savingAccountHasInterestRateDaoImpl.getInterestRateByAccountNumberID(customerAccount.getAccountNumber());
			//double interestAmount = currentBalance - accountHasInterestRate.getBalance();
			amountTemp = accountHasInterestRate.getBalance();
			return amountTemp + amountTemp*numberDays*interestRate/36000;
		}
		return currentBalance;
	}

	@Override
	public double getInterestAmount(CustomerAccount customerAccount, SavingTransactionType savingTransactionType){
		double currentBalance = customerAccount.getAccountBalance();
		if (savingTransactionType == SavingTransactionType.SAVING) {
			return 0;
		}else if(savingTransactionType == SavingTransactionType.WITHDRAW_ALL){
			return currentBalance;
		}else if(savingTransactionType == SavingTransactionType.WITHDRAW_INTEREST){
			AccountHasInterestRate accountHasInterestRate = savingAccountHasInterestRateDaoImpl.getInterestRateByAccountNumberID(customerAccount.getAccountNumber());
			double interestAmount = currentBalance - accountHasInterestRate.getBalance();
			return interestAmount;
		}
		return 0;
	}
	

	@Override
	public List<InterestRate> getAllInterestRateByCurrencyID(int currencyID) {
		List<InterestRate> interest = interestRateDao.getAllInterestRateByCurrencyID(currencyID);
		return interest;
	}


	@Override
	public int getNumberOfDays(int periodTypeID) {
		PeriodType periodType = periodTypeDao.getPeriodTypeByID(periodTypeID);
		if(periodType!= null){
			String unit = periodType.getUnit();
			int period = periodType.getSavingPeriod();
			switch (unit) {
			case ProjectBankingConstant.WEEK:
				return period*7;
			case ProjectBankingConstant.MONTH:
				return period*30;
			default:
				return 0;
			}
		}
		return 0;
	}

	@Override
	public Timestamp getExpiryDate(int periodTypeID) {
		int numberOfDays = getNumberOfDays(periodTypeID);
		long miliseconds = 604800000*numberOfDays; //7*24*60*60*1000
		System.out.println(numberOfDays + " " +  miliseconds + " " + miliseconds/604800000);
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_WEEK, numberOfDays);
		Date date = new Date(cal.getTime().getTime());
		Timestamp timestamp = new Timestamp(date.getTime());
		
		return timestamp;
	}

	@Override
	public InterestRate getNewestInterestRateByCurrencyPeriod(int currencyID,
			int periodTypeID) {
		InterestRate interestRate = null;
		if(interestRateDao != null){
			interestRate = interestRateDao.getInterestNewestRateByType(currencyID, periodTypeID);
		}
		return interestRate;
	}

}
